<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-form-item
          :hidden="true"
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['id']"
          />
        </a-form-item>

        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="设备编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="设备编号"
                :disabled="editable || showable"
                v-decorator="['code', {rules: [{required: true, message: '请输入设备编号'}]}]"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="设备名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="设备名称"
                :disabled="showable"
                v-decorator="['name', {rules: [{required: true, message: '请输入设备名称'}]}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="设备描述"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="设备描述"
                :disabled="showable"
                v-decorator="['content']"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="设备状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="设备状态"
                :disabled="showable"
                v-decorator="['status', {rules: [{required: true, message: '请选择设备状态'}]}]"/>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="设备类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="设备类型"
                :disabled="showable"
                v-decorator="['type', {rules: [{required: true, message: '请选择设备类型'}]}]"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>

</template>
<script>
import pick from 'lodash.pick'
import { FormMixin } from '@/mixins/FormMixin'
import { getObj, saveObj } from '@/api/base/device'
import moment from 'moment'

export default {
  name: 'planDayForm',
  mixins: [FormMixin],
  data () {
    return {
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },

  methods: {

    // 获取信息
    edit (id, type) {
      this.visible = true
      const { form: { setFieldsValue, resetFields } } = this
      resetFields()
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(id).then(res => {
          const record = res.data
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id', 'code', 'content','name','status','type']))
          })
        }).catch((err) => {
          this.$message.error(err.msg)
        }).finally(_ => {
          this.confirmLoading = false
        })
      }
    },

    // 结束保存
    handleOk () {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true
          console.log(values,'value')
          try {
            await saveObj(values)
            this.confirmLoading = false
            this.handleCancel()
            this.$emit('ok')
          } catch (e) {
            this.$message.error(e.msg)
            this.confirmLoading = false
          }
        }
      })
    }

  }
}
</script>

<style scoped>

</style>